package com.css.fxfzysx.modules.lifeline.controller;

import com.css.fxfzysx.base.response.RestResponse;
import com.css.fxfzysx.modules.lifeline.entity.newEntity.SmxVersionEntity;
import com.css.fxfzysx.modules.lifeline.param.*;
import com.css.fxfzysx.modules.lifeline.service.LifelineEngineeringService;
import com.css.fxfzysx.modules.lifeline.vo.fxqh.EvaluationResults;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("lifelineEngineering")
@Slf4j
public class LifelineEngineeringController {

    @Autowired
    private LifelineEngineeringService lifelineEngineeringService;

    /**
     * 查询重复数据
     *
     * @param param
     * @return
     */
    @PostMapping("getRepeatData")
    public RestResponse getRepeatData(@RequestBody RegionParam param) {
        try {
            List<SmxVersionEntity> list = lifelineEngineeringService.getRepeatData(param);
            return RestResponse.succeed(list);
        } catch (Exception e) {
            log.error("post getRepeatData error:{}", e.getMessage(), e);
            return RestResponse.fail("查询失败!");
        }
    }

    /**
     * 数据版本
     *
     * @return
     */
    @GetMapping("getVersionCode")
    public RestResponse getVersionCode(RegionParam param) {
        try {
            Map<String, Object> data = lifelineEngineeringService.getVersionCode(param);
            return RestResponse.succeed(data);
        } catch (Exception e) {
            log.error("get getVersionCode error:{}", e.getMessage(), e);
            return RestResponse.fail("查询失败!");
        }
    }

    /**
     * 上传shp
     *
     * @param param
     * @return
     */
    @PostMapping("/uploadShp")
    public RestResponse uploadShp(@RequestBody RegionParam param) {
        RestResponse response = null;
        try {
            Map<String, Object> map = lifelineEngineeringService.uploadShp(param);
            boolean res = ((Boolean) map.get("res")).booleanValue();
            if (res) {
                response = RestResponse.succeed("上传成功!");
                response.setMessage("上传成功!");
            } else {
                response = RestResponse.fail("上传失败!");
                response.setMessage(map.get("mess").toString());
            }
        } catch (Exception e) {
            String errormessage = "上传失败!";
            response = RestResponse.fail(errormessage);
            e.printStackTrace();
        }
        return response;
    }

    /**
     * 风险区划  生命线数据源查询接口
     *
     * @param param
     * @return
     */
    @GetMapping("getLifelineDataSource")
    public RestResponse getLifelineDataSource(PageParam param) {
        try {
            Map<String, Object> data = lifelineEngineeringService.getLifelineDataSource(param);
            return RestResponse.succeed(data);
        } catch (Exception e) {
            log.error("get page error:{}", e.getMessage(), e);
            return RestResponse.fail("查询失败!");
        }
    }

    /**
     * 风险区划  根据版本号查询
     *
     * @return
     */
    @GetMapping("getAnalysisByVersionId")
    public RestResponse getAnalysisByVersionId(AnalysisParam param) {
        try {
            List<EvaluationResults> list = lifelineEngineeringService.getAnalysisByVersionId(param);
            return RestResponse.succeed(list);
        } catch (Exception e) {
            return RestResponse.fail("失败!");
        }
    }


    /**
     * 根据版本号查询
     *
     * @return
     */
    @GetMapping("getByTypeAndVersionId")
    public RestResponse getByTypeAndVersionId(SmxDataParam param) {
        try {
            Integer page = param.getPage();
            Integer size = param.getRows();
            String versionId = param.getVersionId();
            Integer type = param.getType();
            Map<String, Object> data = lifelineEngineeringService.getByTypeAndVersionId(page, size, versionId, type);
            return RestResponse.succeed(data);
        } catch (Exception e) {
            log.error("get page error:{}", e.getMessage(), e);
            return RestResponse.fail("查询失败!");
        }
    }

    /**
     * 根据版本号删除
     */
    @GetMapping("deleteByTypeAndVersionId")
    public RestResponse deleteByTypeAndVersionId(String versionId) {
        try {
            lifelineEngineeringService.deleteByTypeAndVersionId(versionId);
            return RestResponse.succeed("删除成功!");
        } catch (Exception e) {
            log.error("get deleteByTypeAndVersionId error:{}", e.getMessage(), e);
            return RestResponse.fail("删除失败!");
        }
    }


    /**
     * 删除
     *
     * @param type
     * @param ids
     * @return
     */
    @GetMapping("deleteByTypeAndId")
    public RestResponse deleteByTypeAndId(Integer type, String ids) {
        try {
            lifelineEngineeringService.deleteByTypeAndId(type, ids);
            return RestResponse.succeed("删除成功!");
        } catch (Exception e) {
            log.error("get deleteByTypeAndId error:{}", e.getMessage(), e);
            return RestResponse.fail("删除失败!");
        }
    }

    /**
     * 修改
     *
     * @param map
     * @return
     */
    @PostMapping("updateById")
    public RestResponse updateById(@RequestBody HashMap<Integer, Object> map) {
        try {
            lifelineEngineeringService.updateById(map);
            return RestResponse.succeed("修改成功!");
        } catch (Exception e) {
            log.error("post updateById error:{}", e.getMessage(), e);
            return RestResponse.fail("修改失败!");
        }
    }

    /**
     * 风险区划，根据版本号和id查询变电站或基站数据id
     *
     * @param versionId
     * @param type
     * @return
     */
    @GetMapping("getVectorDataIdsByVersionId")
    public RestResponse getVectorDataIdsByVersionId(String versionId, Integer type) {
        try {
            return RestResponse.succeed(lifelineEngineeringService.getVectorDataIdsByVersionId(versionId, type));
        } catch (Exception e) {
            log.error("get getVectorDataIdsByVersionId error:{}", e.getMessage(), e);
            return RestResponse.fail("失败!");
        }
    }

    /**
     * 范围圈选
     *
     * @param param
     * @return
     */
    @PostMapping("getByRange")
    public RestResponse getByRange(@RequestBody CircleParam param) {
        try {
            HashMap<String, Object> list = lifelineEngineeringService.getByRange(param);
            return RestResponse.succeed(list);
        } catch (Exception e) {
            log.error("post getByRange error:{}", e.getMessage(), e);
            return RestResponse.fail("查看失败!");
        }
    }
}
